import { defineStore } from 'pinia'
import { store } from '../index'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { EntState, IEntFields, IEntConditionReq, IEntTag } from '@/types/ent'
import * as EntCompanyApi from '@/api/ent/company'
import { getAdvanceCondtion } from '@/api/ent/advanceSearch/index'
import { simpleList } from '@/api/ent/tag'
const { wsCache } = useCache()

export const useEntStore = defineStore('ent', {
  state: (): EntState => {
    return {
      fields: wsCache.get(CACHE_KEY.ENT_FIELDS) || [],
      tags: [],
      advanceCondList: []
    }
  },
  getters: {
    getFields(): IEntFields[] {
      return this.fields
    },
    getTags(): IEntTag[] {
      return this.tags
    }
  },
  actions: {
    async setFields(data: IEntConditionReq) {
      // 先检查缓存， 如果缓存中有， 就不再api
      //if (!wsCache.get(CACHE_KEY.ENT_FIELDS)) {
      const { fields } = await EntCompanyApi.getQueryCondition(data)
      this.fields = fields
      wsCache.set(CACHE_KEY.ENT_FIELDS, fields)
      // }
    },
    async setTags() {
      const data = await simpleList()
      this.tags = data
      // wsCache.set(CACHE_KEY.ENT_TAG, data)
    },
    async setAdvanceCond() {
      const data = await getAdvanceCondtion()
      this.advanceCondList = data
    }
  }
})

export const useEntStoreWithOut = () => {
  return useEntStore(store)
}
